package small.woods.msRedis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import small.woods.msRedis.service.DistributedRedisLockService;
import small.woods.msRedis.utils.DistributedRedisLockUtil;

import java.util.Date;
import java.util.concurrent.TimeUnit;


/**
 * @description: 布式锁---redisson分布式锁代码实现
 * @author: yutingting
 * @create: 2022-01-19 09:52
 **/
@Service
public class DistributedRedisLockServiceImpl implements DistributedRedisLockService {
    public final static String remark = "机器3:";

    @Autowired
    DistributedRedisLockUtil distributedRedisLockUtil;

    @Override
    public void executeDistributedRedisLock() {
        String key = "lock";
        boolean lock = false;
        try {
            lock = distributedRedisLockUtil.acquire(key);
            if (lock){
                System.out.println("redisson---我已经获取到锁了");
                System.out.println(remark + new Date());
                TimeUnit.SECONDS.sleep(2);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (lock){
                distributedRedisLockUtil.release(key);
            }
        }
    }
}
